var data = [{'id':'1','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'2','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'3','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'11','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'4','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'12','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'5','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'13','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'6','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'14','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'7','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'15','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'8','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'16','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'9','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'17','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}
    ,{'id':'10','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'},{'id':'18','text1':'text1','text2':'text2','text3':'text3','text4':'text4','text5':'text5','text6':'text6','text7':'text7','text8':'text8','text9':'text9','text10':'text10','text11':'text11','text12':'text12','text13':'text13'}];
/*let chineselogo;
let englishlogo;*/
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,upload = layui.upload,
    form = layui.form
    ,$ = layui.jquery
    ,pid=getQueryVariable("pid")//主数据 中的pid，即公司表中code
    ,id=getQueryVariable("id")//部门表中id
;
var insertandupdateflag="N";//N:新增;M :修改

var zhuData={};//主数据数据源
var logData=[];//操作日志数据源
var bankData=[];//银行数据源
var sealData=[];//印章数据
var bankcurrId="";//银行修改删除时id
getDepartment();//根据部门id，获取部门名称
getseal();//获取印章
if(id==""){//新增主数据

}else{//修改主数据
    getCompanyData();//获取主数据
}

//公司主信息
/*form.val("component-form",{
    "componentname":"公司名称"
})*/

//中文logo
upload.render({
    elem: '#chinalogoadd'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"chineselogo"}//头像
    ,before: function(obj) {
        /*var thin = this;
        obj.preview(function(index, file, result){
            $("#"+thin.id+"").attr('src', result); //图片链接（base64）
        });*/

    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#chinalogo").attr('src',src);
            $("input[name='chinalogo']")[0].value=src;

        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//删除
$("#chinalogodel").on('click',function () {
    $("#chinalogo").attr('src','');
    // headimg = "";
    $("input[name='chinalogo']")[0].value="";
})
$("#enginshlogodel").on('click',function () {
    $("#enginshlogo").attr('src','');
    // headimg = "";
    $("input[name='enginshlogo']")[0].value="";
})
//英文logo
upload.render({
    elem: '#enginshlogoadd'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"enginshlogo"}//头像
    ,before: function(obj) {
        /*var thin = this;
        obj.preview(function(index, file, result){
            $("#"+thin.id+"").attr('src', result); //图片链接（base64）
        });*/

    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#enginshlogo").attr('src',src);
            $("input[name='enginshlogo']")[0].value=src;

        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//---开户银行
table.render({
    elem: '#banktables'
    //,data:data
    ,type:"GET"
    , url:'bank/selectByCompanyCode'
    ,where:{
        "CompanyCode":pid
    }
    ,cols: [[
        {checkbox: true, fixed: true}
        /*,{field:"id",function(){
                $("[data-field=‘id’]").css('display','none');
            }
          }*/
        ,{field:"bankcode",title:"银行编码",width:100, fixed: true}
        ,{field:"companyCode",title:"所属公司编码",width:100, fixed: true}
        ,{field:'bankname', title:'银行名称', width:100, fixed: true}
        ,{field:'englishname', title:'英文名称', width:100}
        ,{field:'address', title:'地址', width:100}
        ,{field:'englishaddress', title:'英文地址', width:100}
        ,{field:'swiftno', title:'Swif号', width:100}
        ,{field:'bankaccount', title:'银行账号', width:100}
        ,{field:'bankaccountName', title:'账号姓名', width:100}
        ,{field:'bizhong', title:'币种'}
        ,{field:'forBusiness', title:'业务用'}
        ,{field:'forCustomsdeclaration', title:'报关用'}
        ,{field:'forBilling', width:100, title:'开票用'}
        ,{field:'defaultbusinessuse', width:100, title:'默认业务'}
        ,{field:'stopuse', title:'停用'}
        ,{fixed: 'right', width:158, align:'center', toolbar: '#bankeditor', title:'操作'}
    ]]
    ,id: 'banktableid'
    ,height: $(document).height() - $('#banktables').offset().top - 50
    ,limit:30//显示的数量
    ,page:true
});
//银行监听工具条
table.on('tool(banktable)',function (obj) {
    console.log(obj);
    var data = obj.data;
    if(obj.event === 'del'){

           // obj.del();
            if(  bankdel(obj.data.id,true)){
                layer.msg("删除成功");
                table.reload('banktableid', {
                    url:'bank/selectByCompanyCode',
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where:{
                        "CompanyCode":pid
                    }
                }, 'data');
            }else{
                layer.msg("删除失败");
            }



    } else if(obj.event === 'edit'){
        layer.open({
            type: 1,
            title: "修改开户银行信息",
            area: ['685px', '635px'],
            content: $("#banktUpdate")//引用的弹出层的页面层的方式加载修改界面表单
        });
        //动态向表传递赋值可以参看文章进行修改界面的更新前数据的显示，当然也是异步请求的要数据的修改数据的获取
        setFormValue(obj,data);
    }
})
var $ = layui.$, active = {
    getCheckData: function(){ //添加
        layer.open({
            type: 1,
            title: "添加开户银行信息",
            area: ['685px', '635px'],
            content: $("#banktUpdate")//引用的弹出层的页面层的方式加载修改界面表单
        });
        setFormValuetj();
    }
    ,getCheckLength: function(){ //删除

        var checkStatus=table.checkStatus('banktableid'),
            data=checkStatus.data,
            delList=[];
        if(checkStatus.data.length==0){
            layer.msg("请选择一条数据！");
            return false;
        }
        data.forEach(function(n,i){
            delList.push(n.id);
        });

                if (bankdel(delList, false)) {
                    layer.msg("删除成功");
                    table.reload('banktableid', {
                        url: 'bank/selectByCompanyCode',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            "CompanyCode": pid
                        }
                    }, 'data');
                } else {
                    layer.msg("删除失败");
                }



    }

};
$('.bankTablebutton .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
});

//银行新增
$("#banktAddsubmit").on("click",function (data) {
    insert_bank();

    table.reload('banktableid', {
        url:'bank/selectByCompanyCode',
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where:{ "CompanyCode":pid}
    }, 'data');

    layer.closeAll();
});
//银行修改
 $("#banktUpdatesubmit").on("click",function (data) {

     update_bank();
        table.reload('banktableid', {
            url:'bank/selectByCompanyCode',
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where:{ "CompanyCode":pid}
        }, 'data');

        layer.closeAll();
    });
 //印章提交
$("#sealsubmit").on("click",function () {
    var companychapterid=$("input[name='companychapterid']")[0].value;
    var companychapter=$("input[name='companychapter']")[0].value;
    if(companychapter !=""){
        if(insert_seal(companychapterid,companychapter,pid,1)){
            layer.msg("公司章保存成功");
        }else{
            layer.msg("公司章保存失败");
        }
    }
    var defaultcompanychapterid=$("input[name='defaultcompanychapterid']")[0].value;
    var defaultcompanychapter=$("input[name='defaultcompanychapter']")[0].value;
    if(defaultcompanychapter !=""){
        if(insert_seal(defaultcompanychapterid,defaultcompanychapter,pid,2)){
            layer.msg("默认公司章保存成功");
        }else{
            layer.msg("默认公司章保存失败");
        }
    }
    var contractid=$("input[name='contractid']")[0].value;
    var contract=$("input[name='contract']")[0].value;
    if(contract !=""){
        if(insert_seal(contractid,contract,pid,3)){
            layer.msg("销售合同章保存成功");
        }else{
            layer.msg("销售合同章保存失败");
        }
    }
    var contractsealid=$("input[name='contractsealid']")[0].value;
    var contractseal=$("input[name='contractseal']")[0].value;
    if(contractseal !=""){
        if(insert_seal(contractsealid,contractseal,pid,4)){
            layer.msg("采购合同章保存成功");
        }else{
            layer.msg("采购合同章保存失败");
        }
    }
    var documentsid=$("input[name='documentsid']")[0].value;
    var documents=$("input[name='documents']")[0].value;
    if(documents !=""){
        if(insert_seal(documentsid,documents,pid,5)){
            layer.msg("单证专用章保存成功");
        }else{
            layer.msg("单证专用章保存失败");
        }
    }
    var finance=$("input[name='finance']")[0].value;
    var financeid=$("input[name='financeid']")[0].value;
    if(finance !=""){
        if(insert_seal(financeid,finance,pid,6)){
            layer.msg("财务专用章保存成功");
        }else{
            layer.msg("财务专用章保存失败");
        }
    }



  //  1:公司章；2：默认公司章；3：销售合同章；4：采购合同章；5：单证专用章；6：财务专用章
    getseal();

});
function  setseal(data) {
    for(var i=0;i<data.length;i++){
        var da=data[i];
        if(da.type==1){
            $("input[name='companychapterid']")[0].value=da.id;
            $("input[name='companychapter']")[0].value=da.img;
            $("#companychapter").attr("src",da.img);
        }
        if(da.type==2){
            $("input[name='defaultcompanychapterid']")[0].value=da.id;
            $("input[name='defaultcompanychapter']")[0].value=da.img;
            $("#defaultcompanychapter").attr("src",da.img);
        }
        if(da.type==3){
            $("input[name='contractid']")[0].value=da.id;
            $("input[name='contract']")[0].value=da.img;
            $("#contract").attr("src",da.img);
        }
       if(da.type==4){
           $("input[name='contractsealid']")[0].value=da.id;
           $("input[name='contractseal']")[0].value=da.img;
           $("#contractseal").attr("src",da.img);
       }
       if(da.type==5){
           $("input[name='documentsid']")[0].value=da.id;
           $("input[name='documents']")[0].value=da.img;
           $("#documents").attr("src",da.img);
       }
      if(da.type==6){
          $("input[name='finance']")[0].value=da.id;
          $("input[name='financeid']")[0].value=da.img;
          $("#finance").attr("src",da.img);
      }
    }

}
//获取印章
function  getseal() {
    $.ajax({
        type : 'GET',
        data: {"CompanyCode":pid},
        url:'seal/selectByCompanyCode',
        dataType:"json",
        // contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                if(da.data.length >0){
                    setseal(da.data);
                }
            }
        },
        error: function(data) {
            layer.msg("保存失败!");
        }
    });
}
function insert_seal(id,img,companyCode,type){
    var tf=false;
    $.ajax({
        type : 'POST',
        data: {"id":id,"img":img,"type":type,"companyCode":companyCode},
        url:'seal/insert',
        dataType:"json",
       // contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                if(da.data !=0){
                    tf=true;
                }
            }
        },
        error: function(data) {
            layer.msg("保存失败!");
        }
    });
    return tf;
}
//提交按钮监听主数据
form.on('submit(component-form)', function(data){
    data.field.id=id;
    data.field.pid=pid;
    inser(data);
});
function setFormValuetj(){
    $("#banktAddsubmit").css("display","inline-block")
    $("#banktUpdatesubmit").css("display","none")
    $("#banktUpdate-form")[0].reset();
    layui.form.render();
}
//银行修改时回显
function setFormValue(obj,data){
    $("#banktAddsubmit").css("display","none")
    $("#banktUpdatesubmit").css("display","inline-block")
    form.val("banktUpdate-form",{
        "bankname":data.bankname,
        "bankenglishname":data.englishname,
        "bankaddress":data.address,
        "bankenglishaddress":data.englishaddress,
        "swiftno":data.swiftno,
        "bankaccount":data.bankaccount,
        "bankaccountName":data.bankaccountName,
        "bizhong":data.bizhong,
        "bankcode":data.bankcode,
        "forBusiness":data.forBusiness,
        "forCustomsdeclaration":data.forCustomsdeclaration,
        "forBilling":data.forBilling,
        "defaultbusinessuse":data.defaultbusinessuse,
        "stopuse":data.stopuse

    });
    bankcurrId=data.id;

}

var sealid="";
//---其他章
layer.photos({
    photos: '#chapter'
    ,anim: 5
});
upload.render({
    elem: '.chapterimg'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
        var thin = this;
        obj.preview(function(index, file, result){
            $("#"+thin.id+"").attr('src', result); //图片链接（base64）
            sealid=thin.id;
        });

    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#"+sealid+"").attr('src',src);
            $("input[name='"+sealid+"']")[0].value=src;
        }
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
/*//公司印章上传
upload.render({
    elem: '#gsseal'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
        var thin = this;
        obj.preview(function(index, file, result){
            $("#"+thin.id+"").attr('src', result); //图片链接（base64）
        });
    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#gsseal").attr('src',src);
            $("input[name='gsseal']")[0].value=src;
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//公司默认印章上传
upload.render({
    elem: '#defaultcompanychapter'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#defaultcompanychapter").attr('src',src);
            $("input[name='mrseal']")[0].value=src;
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//公司销售印章上传
upload.render({
    elem: '#contract'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#contract").attr('src',src);
            $("input[name='xsseal']")[0].value=src;
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//公司采购合同印章上传
upload.render({
    elem: '#contractseal'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#contractseal").attr('src',src);
            $("input[name='cgseal']")[0].value=src;
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//公司单证印章上传
upload.render({
    elem: '#documents'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#documents").attr('src',src);
            $("input[name='dzseal']")[0].value=src;
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})
//公司财务印章上传
upload.render({
    elem: '#finance'
    , url: 'ftp/uploadFile'
    , field: "uploadify"     //添加这个属性与后台名称保存一致
    , accept: 'file' //允许上传的文件类型
    ,data:{"fileType":"seal"}//头像
    ,before: function(obj) {
    }
    ,done: function(res){
        //如果上传失败
        if(res.status !=200){
            return layer.msg('上传失败');
        }else{
            var ftpPath=res.data[0].ftpPath;
            var fileName=res.data[0].fileName;
            var src=getFtpPath(ftpPath,fileName);
            $("#finance").attr('src',src);
            $("input[name='cwseal']")[0].value=src;
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
})*/
// 下载
function downloadIamge(selector, name) {
    // 通过选择器获取img元素
    var img = document.querySelector(selector)
    // 将图片的src属性作为URL地址
    var url = img.src
    var a = document.createElement('a')
    var event = new MouseEvent('click')

    a.download = name || '下载图片名称'
    a.href = url

    a.dispatchEvent(event)
}
//印章另存
$(".saveimg").on("click",function () {
    downloadIamge("#"+this.getAttribute("data-id"), "图片名称");
})
//印章删除
$(".deleimg").on("click",function () {
    var id= $("input[name='"+$("#"+this.getAttribute("data-id"))[0].id+"id']")[0].value
    if(delseal(id)){
        layer.msg("删除成功");
        $("#"+this.getAttribute("data-id")).attr("src","");

        $("input[name='"+$("#"+this.getAttribute("data-id"))[0].id+"']")[0].value="";
    }else{
        layer.msg("删除失败");
    }


})
//印章删除
function delseal(sid){
    var tf=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:{"id":sid},
        url:'seal/delete',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
               tf=da.data==1?true:false;
            }
        },
        error: function(data) {
            //layer.msg("失败!");
        }
    });
    return tf;
}

//---操作日志
table.render({
    elem: '#operationlog'
   // ,data:data
    ,type:"GET"
    ,url:"OperationLog/selectByPage"
    ,where:{
        "companyCode":pid
    }
    ,cols: [[
        {field:'type', title:'操作类型', fixed: true}
        ,{field:'operator', title:'操作员'}
        ,{field:'updateTime', title:'操作日期'}
        ,{field:'desc', title:'修改内容'}
    ]]
    ,id: 'logtableid'
    ,height: $(document).height() - $('#operationlog').offset().top - 50
    ,limit:30//显示的数量
    ,page:true
});

function getchecked(neme) {
    var getchecked = "";
    $('input[name="'+neme+'"]:checked').each(function(index, domEle){
        getchecked += $(this).val()+",";
    });
    getchecked = getchecked.substr(0, getchecked.length - 1);
    return getchecked;
}
function setchecked(data,name) {
    var unitType = data.split(",");
    var unitTypeCheckbox = $("input[name='"+name+"']");
    for (var i = 0; i < unitTypeCheckbox.length; i++) {
        unitTypeCheckbox[i].checked = false;
    }
    for (var j = 0; j < unitType.length; j++) {
        for (var i = 0; i < unitTypeCheckbox.length; i++) {
            //这里应该是判断title等有正式数据可以改一下
            if (unitTypeCheckbox[i].value == unitType[j]) {
                unitTypeCheckbox[i].value = unitType[j];
                unitTypeCheckbox[i].checked = true;
            }
        }
    }
    form.render();
}
//主提交按钮事件
/*$("#componentsubmit").on("click",function (data) {
    data.field.id=id;
    data.field.pid=pid;
    inser(data);
   /!* var companyForm = decodeURIComponent($("#companyForm").serialize(),true);
    var banktables =  layui.table.cache["banktableid"]
    var imgsrc = [];
    $("td[class=chapterimg] img").each(function() {
        imgsrc.push($(this).attr("src"));
    });

    //companyForm 为表单数据 banktables为开户银行数据 imgsrc为其他章的图片路径
    layer.msg("保存成功", {icon: 6});
    setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
        parent.detab("views/jsp/SystemCenter/organization.jsp");
        parent.intab("views/jsp/SystemCenter/organization.jsp","组织结构")
        parent.detab("views/jsp/SystemCenter/tpl/newupcompanylist.jsp"+(pid==''?'':'?pid='+pid));
    },1000)
    return false;*!/
})*/
//新增 主提交
function inser(data) {
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
  if( insert_zhu(data)){
      layer.msg("主数据提交成功");
  }else{
      layer.msg("主数据提交失败");
  }//保存失败
                            //银行
                          /* if(insert_bank(data)){
                               layer.msg("银行数据提交成功");
                           }else{
                               layer.msg("银行数据交失败");
                           }*/
    //印章
   /* if(insert_seal(data)){
        layer.msg("印章数据提交成功");
    }else{
        layer.msg("印章数据提交成功");
    }*/
    //insert_log(data);//日志
}
//修改时更新
function update() {
    update_zhu();//
    update_bank();//
    update_seal();//
    update_log();//
}
//修改时回显数据
function  getCompanyData() {
    zhu();//获取主数据信息
   // getDepartment();//获取公司名称
   // getBank();//获取银行信息
    //getSeal();//获取印章信息
    //getlog();//获取操作日志
}
function getlog() {
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"CompanyCode":pid},
        url:'OperationLog/selectByCompanyCode',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){

            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function getSeal() {
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"CompanyCode":pid},
        url:'seal/selectByCompanyCode',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){

            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function  getBank() {
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"CompanyCode":pid},
        url:'bank/selectByCompanyCode',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                var banks=da.data;
                data=setBank(banks);
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function setBank(data) {
//data=[{'id':'10','text1':'text1'}]
     var object=[];
    for(var i=0;i<data.length;i++){
        var obj={};
        obj.bankcode=data[i].bankcode;
    obj.companyCode=pid;
    obj.bankname=data[i].bankname;
    obj.englishname=data[i].englishname;
    obj.address=data[i].address;
    obj.englishaddress=data[i].englishaddress;
    obj.swiftno=data[i].swiftno;
    obj.bankaccount=data[i].bankaccount;
    obj.bankaccountName=data[i].bankaccountName;
    obj.bizhong=data[i].bizhong;
    obj.forBusiness=data[i].forBusiness;
    obj.forCustomsdeclaration=data[i].forCustomsdeclaration;
    obj.forBilling=data[i].forBilling;
    obj.defaultbusinessuse=data[i].defaultbusinessuse;
    obj.stopuse=data[i].stopuse
        object.push(obj);
    }
    return object;
}
function zhu() {
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"pid":pid},
        url:'company/selectByPid',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                //如取回值 ，对id赋值
                if(da.data.length==1){
                    var data=da.data[0];
                    setzhu(data);
                    insertandupdateflag="M";
                }else{
                    insertandupdateflag="N";
                }

            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}
function setzhu(data) {
   $("input[name='name']")[0].value=data.name;
    $("input[name='englishname']")[0].value=data.englishname;
    $("input[name='abbreviation']")[0].value=data.abbreviation;
    $("input[name='address']")[0].value=data.address;
    $("input[name='englishaddress']")[0].value=data.englishaddress;
  $("input[name='postalcode']")[0].value=data.postalcode;
    $("input[name='customsno']")[0].value=data.customsno;
    $("input[name='taxregistrationno']")[0].value=data.taxregistrationno;
    $("input[name='formano']")[0].value=data.formano;
     $("input[name='corno']")[0].value=data.corno;
     $("input[name='legalperson']")[0].value=data.legalperson;
     $("input[name='mailiaddress']")[0].value=data.mailiaddress;
   $("input[name='tel']")[0].value=data.tel;
     $("input[name='fax']")[0].value=data.fax;
     $("input[name='homepage']")[0].value=data.homepage;
     $("input[name='newspapersw']")[0].value=data.newspapersw;
     $("input[name='natureofcompany']")[0].value=data.natureofcompany;
     $("input[name='businessscope']")[0].value=data.businessscope;
     $("input[name='chinalogo']")[0].value=data.chinalogo;
     $("#chinalogo").attr("src",data.chinalogo);
     $("input[name='enginshlogo']")[0].value=data.enginshlogo;
     $("#enginshlogo").attr("src",data.enginshlogo);
     /*if(data.isproxy=="on"){
         $("input[name='isproxy']")[0].checked=true;
     }else{
         $("input[name='isproxy']")[0].checked=false;
     }*/
     //$("input[name='isproxy']")[0].value=data.isproxy;
     $("input[name='policyno']")[0].value=data.policyno;
     id=data.id;
     pid=data.pid;
    if(data.isproxy ==1)   {
        $("input[name='isproxy']")[0].checked=true;
       // $("#isproxy").attr("checked", "checked");
    }


    /*form.val("companyForm",{
        "isproxy":data.isproxy==1?true:false
    })*/
    form.render();
}
function  getDepartment() {
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:{"id":id},
        url:'department/selectByKey',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                //公司主信息
                form.val("component-form",{
                    "name":da.data.name
                })
                //form.render();
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
}

function getzhu(data) {
   data.field.name= $("input[name='name']")[0].value;
    data.field.englishname= $("input[name='englishname']")[0].value;
    data.field.abbreviation= $("input[name='abbreviation']")[0].value;
    data.field.address= $("input[name='address']")[0].value;
    data.field.englishaddress= $("input[name='englishaddress']")[0].value;
    data.field.postalcode= $("input[name='postalcode']")[0].value;
    data.field.customsno= $("input[name='customsno']")[0].value;
    data.field.taxregistrationno= $("input[name='taxregistrationno']")[0].value;
    data.field.formano= $("input[name='formano']")[0].value;
    data.field.corno= $("input[name='corno']")[0].value;
    data.field.legalperson= $("input[name='legalperson']")[0].value;
    data.field.mailiaddress= $("input[name='mailiaddress']")[0].value;
    data.field.tel= $("input[name='tel']")[0].value;
    data.field.fax= $("input[name='fax']")[0].value;
    data.field.homepage= $("input[name='homepage']")[0].value;
    data.field.newspapersw= $("input[name='newspapersw']")[0].value;
    data.field.natureofcompany= $("input[name='natureofcompany']")[0].value;
    data.field.businessscope= $("input[name='businessscope']")[0].value;
    data.field.chinalogo= $("input[name='chinalogo']")[0].value;
    data.field.enginshlogo= $("input[name='enginshlogo']")[0].value;
    data.field.isproxy= $("input[name='isproxy']")[0].checked==true?"1":"2";//$("input[name='isproxy']")[0].value;
    data.field.policyno= $("input[name='policyno']")[0].value;
    if(id !="")    data.field.id=id;
    return data;

}
function getnewbank(data){
   data.bankname=$("input[name='bankname']")[0].value;
    data.englishname=$("input[name='bankenglishname']")[0].value;
    data.address=$("input[name='bankaddress']")[0].value;
    data.englishaddress=$("input[name='bankenglishaddress']")[0].value;
    data.swiftno=$("input[name='swiftno']")[0].value;
    data.bankaccount=$("input[name='bankaccount']")[0].value;
    data.bankaccountName=$("input[name='bankaccountName']")[0].value;

    data.bizhong=$("select[name='bizhong']")[0].value;
    data.bankcode=$("input[name='bankcode']")[0].value;

    data.forBusiness=$("input[name='forBusiness']")[0].checked==true?1:0;
    data.forCustomsdeclaration=$("input[name='forCustomsdeclaration']")[0].checked==true?1:0;
    data.forBilling=$("input[name='forBilling']")[0].checked==true?1:0;
    data.defaultbusinessuse=$("input[name='defaultbusinessuse']")[0].checked==true?1:0;
    data.stopuse=$("input[name='stopuse']")[0].checked==true?1:0;
return data;
}
function  insert_zhu(data) {
    data=getzhu(data);
    data=data.field;
    var tf=false;
    if(insertandupdateflag=="M"){
        if(update_zhu(data)){
            tf=true;
        }
        return  tf;
    }

    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify({"pid":data.pid,"name":data.name,"englishname":data.englishname
              ,"abbreviation":data.abbreviation,"address":data.address
            ,"englishaddress":data.englishaddress,"postalcode":data.postalcode
            ,"customsno":data.customsno,"taxregistrationno":data.taxregistrationno
            ,"formano":data.formano,"corno":data.corno
            ,"legalperson":data.legalperson,"mailiaddress":data.mailiaddress
            ,"tel":data.tel,"fax":data.fax
            ,"homepage":data.homepage,"newspapersw":data.newspapersw
            ,"natureofcompany":data.natureofcompany,"businessscope":data.businessscope
            ,"chinalogo":data.chinalogo,"enginshlogo":data.enginshlogo
            ,"isproxy":data.isproxy,"policyno":data.policyno}),
        url:'company/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                if(da.data==1){
                    tf=true;
                }
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}
function  insert_bank() {
    var data={};
    data=getnewbank(data);
    data=data;
    var tf=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify({
            "bankcode":data.bankcode,"companyCode":pid,"bankname":data.bankname,"englishname":data.englishname,
            "address":data.address,"englishaddress":data.englishaddress,"swiftno":data.swiftno,"bankaccount":data.bankaccount,
            "bankaccountName":data.bankaccountName,"bizhong":data.bizhong,"forBusiness":data.forBusiness,
            "forCustomsdeclaration":data.forCustomsdeclaration,"forBilling":data.forBilling,
            "defaultbusinessuse":data.defaultbusinessuse,"stopuse":data.stopuse
        }),
        url:'bank/insert2',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=true;
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}

function  insert_log(data) {

    var tf=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify(data.field),
        url:'OperationLog/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=true;
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}
////
function  update_zhu(data) {

    var tf=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify({"pid":data.pid,"name":data.name,"englishname":data.englishname
            ,"abbreviation":data.abbreviation,"address":data.address
            ,"englishaddress":data.englishaddress,"postalcode":data.postalcode
            ,"customsno":data.customsno,"taxregistrationno":data.taxregistrationno
            ,"formano":data.formano,"corno":data.corno
            ,"legalperson":data.legalperson,"mailiaddress":data.mailiaddress
            ,"tel":data.tel,"fax":data.fax
            ,"homepage":data.homepage,"newspapersw":data.newspapersw
            ,"natureofcompany":data.natureofcompany,"businessscope":data.businessscope
            ,"chinalogo":data.chinalogo,"enginshlogo":data.enginshlogo
            ,"isproxy":data.isproxy,"policyno":data.policyno,"id":data.id}),
        url:'company/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                if(da.data==1){
                    tf=true;
                }
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}
//更新银行信息
function  update_bank() {
    var data={};
    data=getnewbank(data);
    data=data;
    var tf=false;
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data:JSON.stringify({
            "bankcode":data.bankcode,"companyCode":pid,"bankname":data.bankname,"englishname":data.englishname,
            "address":data.address,"englishaddress":data.englishaddress,"swiftno":data.swiftno,"bankaccount":data.bankaccount,
            "bankaccountName":data.bankaccountName,"bizhong":data.bizhong,"forBusiness":data.forBusiness,
            "forCustomsdeclaration":data.forCustomsdeclaration,"forBilling":data.forBilling,
            "defaultbusinessuse":data.defaultbusinessuse,"stopuse":data.stopuse,"id":bankcurrId
        }),
        url:'bank/update',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=true;
            }
        },
        error: function(data) {
            layer.msg("初始化失败!");
        }
    });
    return tf;
}
function  update_seal() {

}
function  update_log() {

}
//银行删除
function bankdel(ids,tf) {
    var flag=false;
    var delids=[];
    if(tf){//单条删除
        delids.push(ids);
    }else{//多条删除
        delids=ids;
    }
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data:{"ids":JSON.stringify(delids)},
        url:'bank/delete',
        dataType:"json",
       //  contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                flag=da.data>0?true:false;
            }
        },
        error: function(data) {
            layer.msg("删除失败!");
        }
    });
    return flag;
}
/**
 * 组装图片回显路径
 */
function getFtpPath(ftpPath,fileName){

    return "ftp/downloadimg"+"?pathname="+ftpPath+"&filename="+fileName;
}